package com.whfc.ms.api.fmam.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fmam.entity.dto.FmamWarehouseDTO;
import com.whfc.fmam.entity.qo.WarehouseAddQO;
import com.whfc.fmam.entity.qo.WarehouseEditQO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.fmam.service.WarehouseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: ；likang
 * @Description: 仓库接口
 * @Date:Create：in 2019/11/14 14:29
 * @Version：1.0
 */
@RequiresPermissions("fmam/warehouse")
@RestController
@RequestMapping("/ms/api/fmam/warehouse")
public class FmamWarehouseController extends BaseController {

    @Autowired
    private WarehouseService warehouseService;

    /**
     * 仓库列表页
     *
     * @param user
     * @param pageNum
     * @param pageSize
     * @param deptId
     * @return
     */
    @GetMapping(value = "/list", params = {"pageNum", "pageSize"})
    public Result listWarehouse(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestParam("pageNum") Integer pageNum,
                                @RequestParam("pageSize") Integer pageSize,
                                @RequestParam(value = "deptId") Integer deptId) {
        logger.info("仓库列表页，loginer:{},pageNum:{},pageSize:{},deptId:{}", user.getUsername(), pageNum, pageSize, deptId);
        PageData<FmamWarehouseDTO> pageData = warehouseService.listFmamWarehouseDTO(user, pageNum, pageSize, deptId);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/list")
    public Result listWarehouse(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestParam(value = "deptId") Integer deptId) {
        logger.info("仓库列表页(不分页)，loginer:{},deptId:{}", user.getUsername(), deptId);
        ListData<FmamWarehouseDTO> pageData = warehouseService.listFmamWarehouseDTO(deptId);
        return ResultUtil.success(pageData);
    }

    /**
     * 新增仓库
     *
     * @param
     * @param request
     * @return
     */
    @RequiresPermissions("fmam/warehouse/add")
    @PostMapping(value = "/add")
    public Result addWarehouse(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @Validated @RequestBody WarehouseAddQO request) {
        logger.info("新增仓库，loginer:{},data:{}", user.getUsername(), request);
        warehouseService.addWarehouse(request);
        return ResultUtil.success();
    }

    /**
     * 编辑修改仓库
     *
     * @param
     * @param request
     * @return
     */
    @RequiresPermissions("fmam/warehouse/edit")
    @PostMapping(value = "/edit")
    public Result editWarehouse(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestBody WarehouseEditQO request) {
        logger.info("编辑修改仓库，loginer:{},data:{}", user.getUsername(), request);
        warehouseService.editWarehouse(request);
        return ResultUtil.success();
    }

    /**
     * 启用
     *
     * @param
     * @param id
     * @return
     */
    @RequiresPermissions("fmam/warehouse/enable")
    @PostMapping(value = "/enable/{id}")
    public Result enableWarehouse(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @PathVariable("id") Integer id) {
        logger.info("启用仓库，loginer:{},data:{}", user.getUsername(), id);
        warehouseService.enableWarehouse(id);
        return ResultUtil.success();
    }

    /**
     * 禁用
     *
     * @param
     * @param id
     */
    @RequiresPermissions("fmam/warehouse/enable")
    @PostMapping(value = "/disable/{id}")
    public Result disableWarehouse(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                   @PathVariable("id") Integer id) {
        logger.info("禁用仓库，loginer:{},data:{}", user.getUsername(), id);
        warehouseService.disableWarehouse(id);
        return ResultUtil.success();
    }

    /**
     * 删除仓库
     *
     * @param
     * @param id
     */
    @RequiresPermissions("fmam/warehouse/del")
    @PostMapping(value = "/del/{id}")
    public Result delWarehouse(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @PathVariable("id") Integer id) {
        logger.info("删除仓库，loginer:{},data:{}", user.getUsername(), id);
        warehouseService.delWarehouse(id);
        return ResultUtil.success();
    }

}
